﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor.PackageManager;
using UnityEngine;

public class Construction : MonoBehaviour
{
    public Camera maincam;

    private GameObject conMenu;
    private ProcessManager pM;

    [SerializeField] private bool isMenuExist = false;
    // Start is called before the first frame update
    void Start()
    {
        maincam = Camera.main;
        conMenu = Resources.Load("Prefab/ConstructionMenu")as GameObject;
        pM = this.gameObject.GetComponent<ProcessManager>();
    }

    // Update is called once per frame
    void Update()
    {
        if (pM.dialogue.dialogIndex >= 4)
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (!isMenuExist)
                {
                    Vector2 mouseScreenPos = Input.mousePosition;
                    //Vector3 tarPos = maincam.ScreenToWorldPoint(mouseScreenPos);
                    Vector3 tarPos = maincam.ViewportToWorldPoint(mouseScreenPos);
                
                    Ray tarRay = maincam.ScreenPointToRay(Input.mousePosition);
                    Debug.Log(tarRay);
                    RaycastHit HitInfo;
                    Physics.Raycast(tarRay, out HitInfo, 999f);
                    if (HitInfo.collider.gameObject.tag == "GroundPlane")
                    {
                        Instantiate(conMenu, HitInfo.point, Quaternion.identity);
                        isMenuExist = true;
                    }

                }
                else
                {
                
                }
            }
        }

    }

    public void killMenu()
    {
        isMenuExist = false;
        Destroy(GameObject.Find("ConstructionMenu(Clone)"));
    }
}
